<?php

declare(strict_types=1);

namespace App\Model\Ai\Atlas;



use App\Model\Ai\Picture;
use App\Model\Ai\Tag\Zh;
use App\Model\Model;
use App\Model\User;

/**
 * @property int $id 
 * @property int $user_id 
 * @property int $total 
 * @property string $type 
 * @property int $rank 
 * @property int $views 
 * @property int $shares 
 * @property int $downloads 
 * @property string $status 
 * @property int $createtime 
 * @property int $updatetime 
 */
class Atlas extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ai_atlas';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['type'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'user_id' => 'integer', 'total' => 'integer', 'rank' => 'integer', 'views' => 'integer', 'shares' => 'integer', 'downloads' => 'integer', 'createtime' => 'integer', 'updatetime' => 'integer'];
	
	
	const CREATED_AT = 'createtime';
	const UPDATED_AT = 'updatetime';
	
	/**
	 * Convert a DateTime to a storable string.
	 *
	 * @return null|string
	 */
	public function fromDateTime(mixed $value): mixed
	{
		return empty($value) ? $value : strval($this->asDateTime($value)->timestamp);
	}
	
	
	public function lists()
	{
		return $this->hasMany(AtlasList::class, 'atlas_id', 'id')->where('status','=','normal');
	}
	
	public function listsa(){
		return $this->belongsToMany(Picture::class,'ai_atlas_list','atlas_id','picture_id');
	}
	
	public function tag()
	{
		return $this->hasMany(AtlasTag::class, 'atlas_id', 'id');
	}
	
	
	public function user()
	{
		return $this->hasOne(User::class, 'id', 'user_id');
	}
	
	public function pictures()
	{
		return $this->belongsToMany(Picture::class,'ai_atlas_list','atlas_id','picture_id');
	}
	
	public function picture()
	{
		return $this->hasOne(Picture::class, 'id', 'picture_id')->where('ai_picture.status','=','normal');
	}
	
	public function tag_zh()
	{
		return $this->hasManyThrough(
			Zh::class,
			AtlasTag::class,
			'atlas_id',
			'label',
			'id',
			'label'
		)->where('ai_tag_zh.status','=','normal');
	}
	
}

